home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3820 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What's wrong here?
  5. Date: Wed, 31 Jan 1996 13:09:39 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9601311209.AA06949@dxmint.cern.ch>
  8. References: <4eml5o$o6h@airdmhor.gen.nz>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. gumboot@airdmhor.gen.nz (Simon Hosie) writes:
  14.  
  15. >   1: #include <stdio.h>
  16. >   2: 
  17. >   3: typedef unsigned short word;
  18. >   4: 
  19. >   5: int main(void)
  20. >   6: {
  21. >   7:     word a, b, c, d;
  22. >   8: 
  23. >   9:     a = 1;
  24. >  10:     b = 2;
  25. >  11:     c = 4;
  26. >  12:     d = a | b | c;
  27. >  13: 
  28. >  14:     d |= a;
  29. >  15: 
  30. >  16:     d |= a | b;
  31. >  17:     d = d | a | b;
  32. >  18: 
  33. >  19:     return 0;
  34. >  20: }
  35. >
  36. >  Watcom gives the following warnings (and _only_ those warnings) compiling
  37. >the above code (except without the line numbers or colons), but only when
  38. >it's compiling it as C++ and generating 32 bit code (GCC doesn't, no matter
  39. >how hard I try).  I don't know C++ myself, but my flatmate is learning it,
  40. >does it have anything to do with the typedef?
  41.  
  42. When you suspect something like this, the easiest thing is to remove the
  43. typedef and see if this changes anything.
  44.  
  45. >TEST.CPP(12): Warning! W389: (col 15) integral value may be truncated during 
  46. >              assignment or initialization 
  47. >TEST.CPP(17): Warning! W389: (col 15) integral value may be truncated during
  48. >              assignment or initialization 
  49.  
  50. There's nothing wrong with the code, the compiler is plain silly (and you
  51. can complain to your vendor).
  52.  
  53. Here's what happens at line 12: the type of the expression a | b | c is
  54. int (not unsigned short, because of the integral promotions) while the
  55. type of d is unsigned short, which in your particular case is a shorter
  56. type.  The compiler notices that it assigns a 32-bit int to a 16-bit
  57. unsigned short and produces the stupid warning, despite the fact that the
  58. result of the expression is guaranteed to fit into 16 bits.
  59.  
  60. But the most blatant stupidity of the compiler is that it warned at line
  61. 17, but it silently accepted line 16, which is identical to 17 for all
  62. intents and purposes.  Both "=" and "|=" are assignment operators, but the
  63. compiler seems to treat them differently when emitting stupid warnings.
  64.  
  65. Dan
  66. -- 
  67. Dan Pop
  68. CERN, CN Division
  69. Email: danpop@mail.cern.ch 
  70. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  71.